package com.vunguyen.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vunguyen.model.Giangvien;
import com.vunguyen.model.Lichlamviec;
import com.vunguyen.model.Phieudangky;
import com.vunguyen.service.LichLamViecService;


@Controller
public class LichLamViecController {
	
	@Autowired
	private LichLamViecService lichlamviecService;
	
	
	@RequestMapping(value = "/lichlamviec", method = RequestMethod.GET)
	public String LichLamViec(Model m, HttpServletRequest request)
	{
		
		m.addAttribute("listLichLamViec",new Lichlamviec());
		m.addAttribute("listLichLamViec", lichlamviecService.getLichLamViec());
		return "lichlamviec";
	}
	@RequestMapping(value = "/lichlamviecgiangvien", method = RequestMethod.GET)
	public String LichLamViecGV(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			m.addAttribute("listLichLamViec",new Lichlamviec());
			m.addAttribute("listLichLamViec", lichlamviecService.getLichLamViec());
			return "lichlamviecgiangvien";
		}
		else{
			return "chuadangnhap";
		}

	}
	@RequestMapping(value = "/themlichlamviec", method = RequestMethod.POST)
	public String ThemLichLamViec(HttpServletRequest request, Model m) throws ParseException{
		
		Lichlamviec  lichlamviec = new  Lichlamviec();
		Giangvien giangvien = new Giangvien();
		int  idcanbo = Integer.parseInt((String) request.getSession().getAttribute("username"));
		lichlamviec.setTuan( Integer.parseInt(request.getParameter("tuan")));
		giangvien.setIdCanBo(idcanbo);
		lichlamviec.setGiangvien(giangvien);
		lichlamviec.setNgay(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngay")));
		lichlamviec.setThoiGianBatDau(new SimpleDateFormat("hh:mm").parse(request.getParameter("thoigianbatdau")));
		lichlamviec.setThoiGianKetThuc(new SimpleDateFormat("hh:mm").parse(request.getParameter("thoigianketthuc")));
		lichlamviec.setNoiDung(request.getParameter("noidung"));
		lichlamviec.setDiaDiem(request.getParameter("diadiem"));
		lichlamviec.setGhiChu(request.getParameter("ghichu"));
		lichlamviecService.addLichLamViec(lichlamviec);
		return "redirect:/lichlamviec-giangvien";
	}
	@RequestMapping(value = "sualichlamviec/{idlichlamviec}")
		public String suaLichLamViec(@PathVariable("idlichlamviec") int idlichlamviec, Model m){
    	List<Lichlamviec> list =  lichlamviecService.getListLichlamviecById(idlichlamviec);
    	m.addAttribute("listLichLamViecForm", list);
    	return "sualichlamviec";
    }
    @RequestMapping(value = "/capnhatlichlamviec", method = RequestMethod.POST)
    public String updateLichLamViec(@RequestParam("idlichlamviec") int idLichLamViec, HttpServletRequest request) throws ParseException
    {
    	Lichlamviec lichlamviecnew =new Lichlamviec();
    	lichlamviecnew.setTuan(Integer.parseInt(request.getParameter("tuan")));
    	lichlamviecnew.setNgay(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngay")));
    	lichlamviecnew.setThoiGianBatDau(new SimpleDateFormat("hh:mm").parse(request.getParameter("thoigianbatdau")));
    	lichlamviecnew.setThoiGianKetThuc(new SimpleDateFormat("hh:mm").parse(request.getParameter("thoigianketthuc")));
    	lichlamviecnew.setNoiDung(request.getParameter("diadiem"));
    	lichlamviecnew.setDiaDiem(request.getParameter("diadiem"));
    	lichlamviecnew.setGhiChu(request.getParameter("ghichu"));
    	lichlamviecService.updateLichLamViec(lichlamviecnew, idLichLamViec);
    	return "redirect:/lichlamviec";
    }
    

	
}
